/**
 * 判断传入的函数是否标记了async
 */
function isAsyncFunction(func){
    return func[Symbol.toStringTag]==='AsyncFunction';
}

// console.log(isAsyncFunction(()=>{}));   //false
isAsyncFunction(async ()=>{}); //true
isAsyncFunction(()=>Promise.resolve()); //false



/**
 * 判断一个值是否是Promise Like?
 * 关键点：Promise A+规范
 */
function isPromiseLike(value){
    return (value!==null&&(typeof value==='object'||typeof value==='function')&&typeof value.then==='function');
}
// console.log(isPromiseLike(()=>Promise.resolve()));

/**
 * let 和 var 的区别
 * 1、使用var 定义变量会污染全局，使用let 不会
 * 2、重复声明，不能混着用
 * 3、暂时性死区
 */

/**
 * 深拷贝的循环引用问题：
 * 
 */
var obj={"a":1,"b":2};
var obj1=JSON.parse(JSON.stringify(obj));
obj.c=obj;
// console.log(obj1,obj);

//正规克隆代码
function deepClone(obj){
    const objctMap=new Map();
    const _deepClone=(value)=>{
        const type=typeof value;
        if(type!=='object'||type===null){
            return value;
        }
        if(objctMap.has(value)){
            return objctMap.get(value);
        }
        const result=Array.isArray(value)?[]:{};
        objctMap.set(value,result);
        for(const key in value){
            result[key]=_deepClone(value[key])
        }
        return result;
    };
    return _deepClone(obj);
}
// console.log(deepClone(obj));


function deepClone1(obj){
    return new Promise((resolve,reject)=>{
        const {port1,port2}=new MessageChannel();
        var obj={"a":1,"b":2};
        port1.postMessage(obj);
        port2.onmessage=(msg)=>{
            resolve(msg.data);
        }
    })
}
var obj={"a":1,"b":2};
console.log(deepClone1(obj));


function deepClone2(newObj, oldObj){
    for(var k in oldObj){
        var item=oldObj[k];
        if(item instanceof Array){
            newObj[k]=[];
            deepClone2(newObj[k],item)
        }else if(item instanceof Object){
            newObj[k]={};
            deepClone2(newObj[k],item)
        }else{
            newObj[k]=item;
        }
    }
}

/**
 * 给fetch实现超时功能
 */ 
function createFetch(timeout){
    return (resource,options)=>{
        let controller=new AbortController();
        options=options||{};
        options=controller.signal;
        setTimeout(()=>{
            controller.abort();
        },timeout)
        return fetch(resource,options);
    }
}
createFetch(100)('https://www.baidu.com');